package com.huika.renrench.control.main.fragment;

import java.util.ArrayList;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.AutoScrollViewPager;
import android.support.v4.view.AutoScrollViewPager.OnPageItemClickListener;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
import com.android.volley.VolleyError;
import com.google.gson.reflect.TypeToken;
import com.huika.renrench.GlobalApp;
import com.huika.renrench.R;
import com.huika.renrench.config.UrlConstants;
import com.huika.renrench.control.common.base.BaseFra;
import com.huika.renrench.control.investment.activity.IncubatorActivity;
import com.huika.renrench.control.login.activity.LoginActivity;
import com.huika.renrench.control.main.epship.acty.EnProjectDetailActivity;
import com.huika.renrench.control.main.epship.acty.EntrepreneurshipProjectActivity;
import com.huika.renrench.control.main.shoptf.act.ShopTransferDetailActivity;
import com.huika.renrench.control.my.merchant.MyProgramActivity;
import com.huika.renrench.control.my.merchant.MyShopActivity;
import com.huika.renrench.control.project.activity.ProjectCaseActivity;
import com.huika.renrench.http.FormResultRequest;
import com.huika.renrench.http.JSONWrapAjaxParams;
import com.huika.renrench.http.RequestResult;
import com.huika.renrench.support.bean.AdvertisementInfo;
import com.huika.renrench.support.bean.AdvertisementInfoList;
import com.huika.renrench.utils.LoginUtils;
import com.huika.renrench.utils.WebNetUtils;
import com.huika.renrench.view.webview.BaseWebViewAct;
import com.nostra13.universalimageloader.core.DisplayImageOptions;

/**
 * @author chenyuan
 * @date：2015年6月1日,下午2:42:30
 * @describe:主页的创业Fragment
 */
public class EntrepreneurshipFragment extends BaseFra {
	/** 创业项目 */
	private RelativeLayout entrepreneurship_projectLoy;
	/** 我有创业项目 */
	private RelativeLayout have_entrepreneurship_projectLoy;
	/** 案例 */
	private RelativeLayout project_case;
	/** 孵化器 */
	private RelativeLayout project_incubator;
	/** 滚动广告 */
	private AutoScrollViewPager<AdvertisementInfo> advertisementVp;
	/** 广告接口没请求完时用于遮挡 */
	private View cover_iv;
	/** 广告底部的点 */
	private LinearLayout advertisement_dotLl;

	@Override
	protected View inflaterView(LayoutInflater inflater, ViewGroup container, Bundle bundle) {
		View root = inflater.inflate(R.layout.frag_entrepreneurship, container, false);
		return root;
	}

	@Override
	protected void initWidget(View parentView) {
		// TODO Auto-generated method stub
		super.initWidget(parentView);

		initViews(parentView);

		initListener();

		// initDatas(parentView);

		executeGetAdvertisementInfoList();
	}

	/**
	 * @author cy
	 * @date 2015年6月2日,上午9:58:05
	 */
	private void initViews(View parentView) {
		// TODO Auto-generated method stub

		entrepreneurship_projectLoy = (RelativeLayout) parentView.findViewById(R.id.entrepreneurship_projectLoy);
		have_entrepreneurship_projectLoy = (RelativeLayout) parentView.findViewById(R.id.have_entrepreneurship_projectLoy);
		project_case = (RelativeLayout) parentView.findViewById(R.id.project_case);
		project_incubator = (RelativeLayout) parentView.findViewById(R.id.project_incubator);

		/** 滚动广告 **/
		advertisementVp = (AutoScrollViewPager<AdvertisementInfo>) parentView.findViewById(R.id.advertisementVp);
		advertisementVp.setScaleType(ScaleType.FIT_XY);
		advertisementVp.setImageOptions(new DisplayImageOptions.Builder().showImageOnLoading(R.drawable.icon_default_small)
		// resource or drawable
				.showImageForEmptyUri(R.drawable.icon_default_small).showImageOnFail(R.drawable.icon_default_small).cacheInMemory(true)// 开启内存缓存
				.cacheOnDisk(true) // 开启硬盘缓存
				.resetViewBeforeLoading(false).build());
		cover_iv = parentView.findViewById(R.id.cover_iv);
		advertisement_dotLl = (LinearLayout) parentView.findViewById(R.id.advertisement_dotLl);
	}

	/**
	 * @author cy
	 * @date 2015年6月2日,上午9:49:51
	 */
	private void initListener() {
		// TODO Auto-generated method stub
		entrepreneurship_projectLoy.setOnClickListener(this);
		have_entrepreneurship_projectLoy.setOnClickListener(this);
		project_case.setOnClickListener(this);
		project_incubator.setOnClickListener(this);

		advertisementVp.setOnPageItemClickListener(new OnPageItemClickListener<AdvertisementInfo>() {

			@Override
			public void onPageItemClickListener(AdvertisementInfo pd) {
				// TODO Auto-generated method stub
				// if (isCanItemClick) {

				switch (pd.type) {
				/** 1.平台广告 */
				case 1:

					if (!TextUtils.isEmpty(pd.content)) {

						if (pd.content.indexOf("www") != -1) {
							// 跳到WEBVIEW 专题页面
							Bundle bundle = new Bundle();
							bundle.putString(BaseWebViewAct.PAGE_URL, setLinkUrl(pd.content));
//							bundle.putString(BaseWebViewAct.PAGE_URL, "http://m.taobao.com/?sprefer=sypc00");
							showActivity(getActivity(), BaseWebViewAct.class, bundle);
						}
						
						/*
						 * else { Intent intent = new Intent( getActivity(),
						 * LessonDetailActivity.class);
						 * intent.putExtra(Constant.COURSE_Id, pd.webUrl);
						 * showActivity(getActivity(), intent); }
						 */

					}

					break;
				/** 2.产品广告 */
				case 2:
					
					
					if (pd.productType==1) {
						if (!TextUtils.isEmpty(pd.content)) {
							// 跳到WEBVIEW 专题页面
							Bundle bundle = new Bundle();
							bundle.putString("projectId", pd.content);
							showActivity(getActivity(), EnProjectDetailActivity.class, bundle);
							
						}
					}else if(pd.productType==2){
						if (!TextUtils.isEmpty(pd.content)) {
							Bundle bundle1 = new Bundle();
							bundle1.putString("shopId", pd.content);
							showActivity(getActivity(), ShopTransferDetailActivity.class, bundle1);
						}
					}
					break;
				default:
					break;
				}

			}
			// }
		});
	}

	/**
	 * 测试数据
	 * 
	 * @author cy
	 * @date 2015年3月26日,上午10:05:31
	 */
	private void initDatas(View parentView) {
		AdvertisementInfoList advertisementInfoList = new AdvertisementInfoList();
		cover_iv.setVisibility(View.GONE);
		// 初始化广告图片数据
		advertisementVp.refreshDatas(advertisementInfoList.getTestData(), (LinearLayout) parentView.findViewById(R.id.advertisement_dotLl), false, true);
		advertisementVp.start(3000);
	}

	@Override
	protected void widgetClick(View v) {
		// TODO Auto-generated method stub
		super.widgetClick(v);
		Intent intent = null;
		switch (v.getId()) {
		case R.id.entrepreneurship_projectLoy:
			/** 创业项目 */
			intent = new Intent(getActivity(), EntrepreneurshipProjectActivity.class);
			startActivity(intent);
			break;
		case R.id.have_entrepreneurship_projectLoy:
			/** 我有创业项目 */
			if (LoginUtils.isLogin()) {
				intent = new Intent(getActivity(), MyProgramActivity.class);
				intent.putExtra("backStr", "创业");
				startActivity(intent);
			} else {
				intent = new Intent(getActivity(), LoginActivity.class);
				startActivity(intent);
			}
			break;
		case R.id.project_incubator:
			/**孵化器*/
			if (LoginUtils.isLogin()) {
				intent = new Intent(getActivity(), IncubatorActivity.class);
				startActivity(intent);
			} else {
				intent = new Intent(getActivity(), LoginActivity.class);
				startActivity(intent);
			}
			break;
		case R.id.project_case:
			/**案例*/
			if (LoginUtils.isLogin()) {
				intent = new Intent(getActivity(), ProjectCaseActivity.class);
				startActivity(intent);
			} else {
				intent = new Intent(getActivity(), LoginActivity.class);
				startActivity(intent);
			}
			break;

		default:
			break;
		}
	}

	/***
	 * 拼接链接地址
	 * 
	 * @author cy
	 * @date 2015年4月20日,上午10:15:11
	 * @param linkUrl
	 * @return
	 */
	private String setLinkUrl(String linkUrl) {
		if (linkUrl.indexOf("http://") != -1) {

		} else {
			linkUrl = "http://" + linkUrl;
		}

		return linkUrl;
	}

	/** 获取广告信息 */
	private void executeGetAdvertisementInfoList() {
//		showLoadingDialog("加载中");
		JSONWrapAjaxParams ajaxParams = new JSONWrapAjaxParams();
		String u_id = GlobalApp.getInstance().getUserId();
		if (!TextUtils.isEmpty(u_id)) {
			ajaxParams.putCommonTypeParam("userId", u_id);
		} else {
			ajaxParams.putCommonTypeParam("userId", "0");
		}
//		ajaxParams.putCommonTypeParam("type", 1 + "");
		FormResultRequest<ArrayList<AdvertisementInfo>> request = new FormResultRequest<ArrayList<AdvertisementInfo>>(UrlConstants.GETADVERTISEMENTINFO,
				getAdvertisementInfoListReq, getAdvertisementInfoListError, new TypeToken<RequestResult<ArrayList<AdvertisementInfo>>>() {
				}.getType());
		request.setRequestParams(ajaxParams);
		executeRequest(request);
	}

	private Listener<RequestResult<ArrayList<AdvertisementInfo>>> getAdvertisementInfoListReq = new Listener<RequestResult<ArrayList<AdvertisementInfo>>>() {

		@Override
		public void onResponse(RequestResult<ArrayList<AdvertisementInfo>> result) {
			dismissLoadingDialog();
			if (result != null && result.flag == 1 && result.getRs().size() > 0) {
				cover_iv.setVisibility(View.GONE);
				advertisementVp.refreshDatas(result.getRs(), advertisement_dotLl, false, true);
				advertisementVp.start(3000);
			} else {
				cover_iv.setVisibility(View.VISIBLE);
			}

		}
	};

	private ErrorListener getAdvertisementInfoListError = new ErrorListener() {

		@Override
		public void onErrorResponse(VolleyError arg0) {

			// TODO Auto-generated method stub
			cover_iv.setVisibility(View.VISIBLE);
			dismissLoadingDialog();
			// showToastMsg("网络异常！");
		}
	};

	// public void onResume() {
	// executeGetAdvertisementInfoList();
	// };
}
